% GIVES IMPULSE REPSONESE FOR NOISE SHOCKS TFP SHOCKS AND NEWS SHOCSK;
% X-1 DETERMINES NUMBER OF SHOCKS NEWS SHOCK IS ORTHOGONAL TO, 2 is when
% only one shock is orthogonal to news;b is the variable whose fevd you
% want to maximize;
function [IRF_NEWS_WORLD_,IRF_TFP_WORLD_,IRF_istc_WORLD_,s_news_WORLD,s_tfp,s_istc,usa,vd_,ADD,vd_istc,FF,c_tot]=ISTC_NEWS(data,p,H,x,b,h,m,trend_us,trend_uk,rest,st,LE,r1,r2,lr,exo,exo_for,cs,bayes,quadr,fiscal,kilian,bias_term_F,bias_term_C,jmcb_joint,fiscal_new,repl,x1,x2,H1,H2,jedc_joint,tot_process,x3,x4,jie)
% data = xlsread('C:\THIRD_CHAPTER\Canada\FINAL_DATA.xls');
% data = xlsread('C:\THIRD_CHAPTER\FINAL_US_DATA.xls');data=[data rand(length(data),5)];
% p=5;m=10;H=60;x=3;b=2;h=60;
T=length(data); k=length(data(1,:));
%  rest=0;st=4;LE=1;r1=0;trend_us=0;trend_uk=0;
%  p=5; H=60;
%  data1=data(:,[1 3 2 4 5 6 8 7 9 10 11]);
%  data=data1;
  ret=nad(data,0,m,T,p,trend_us,r1,LE,exo,rest,st,bayes,quadr);
sig_x=ret.cov_res;
 coint_x=ret.coint;
 c1=ret.c;
 E=ret.G;
res_x=ret.res;SIG1=ret.SIG;
% max_eig=ret.max_eig;
% trace_stat=ret.trace_stat;
if fiscal_new==1
    data1=data(~data(:,k)==0,:);
    siz=size(data1,1);
ret1=nad(data1,m,k,siz,p,trend_uk,r2,LE,exo_for,rest,st,bayes,0);
else
 ret1=nad(data,m,k,T,p,trend_uk,r2,LE,exo_for,rest,st,bayes,0);
end   
G=ret1.G;
 coint_y=ret1.coint;
sig_y=ret1.cov_res;
c2=ret1.c;
if fiscal_new==1
res_y=[nan(size(ret1.res,1),T-siz) ret1.res];
else
  res_y=ret1.res;  
end
SIG2=ret1.SIG;
 ret2=irf_nad(c1,c2,E,G,r1,r2,sig_x,sig_y,res_x,res_y,coint_x,coint_y,trend_uk,trend_us,m,k-m,T,p,h,LE,lr,rest,st,SIG1,SIG2,bayes,quadr,kilian,bias_term_F,bias_term_C,tot_process);
 s_us=ret2.shocks_us;FF=ret2.FF;c_tot=ret2.c_tot;
%  if bayes==1
%  ret2.SIGMA=[[ret.SIG;zeros(1,m)] zeros(k,1)];
%  end
 
 s_istc=ret2.shocks(:,2);s_tfp=ret2.shocks(:,1);%NOTICE THAT COLUMN NUMBER DETERMINES VARIABLE
%  FOR HD
% c_us=ret2.IRF_US{j};
% c_open=ret2.IRF_UK{j};
% s_open=ret2.shocks;
% % FOR VD
% c_us1=ret2.IRF_US;
% c_open1=ret2.IRF_UK;
%  plot(ret2.IRF_US{1}(:,3:7));
%  legend('FFR','OIL','CEI','SP','RP');
 clear res*   c1 c2 coint* sig* r1 r2 v* E
% ACCOUNTING FOR STATIONARY UNDIIFEFENCED VARIABLES FOR VARIANCE DECOMPSIITON AND IRF (for long run restriciton fisher method - put inflation in the end);
% if rest==1&&LE==0;
%     for i=1:h-1
%   ret2.S_IRF{1,1}=ret2.S_IRF{1,1};
%   ret2.S_IRF{1,i+1}=[ret2.S_IRF{1,i+1}(1:end-st-1,:);ret2.S_IRF{1,i+1}(end-st:end,:)-ret2.S_IRF{1,i}(end-st:end,:)];
%     end
% end
  %  REST OF WORLD NEW SHOCKS DERIVATION;j=H means francis method, j=1 nadav and barsky and sims;
% if bayes==1
%     ret2.S_IRF{1}(k,k)=1;
% end

gg_=zeros(k,k,h);gg=zeros(k,k,H);vv_=zeros(k,k,h);
for i=1:h
    gg_(:,:,i)=ret2.S_IRF{i}*ret2.S_IRF{i}';
vv_(:,:,i)=sum(gg_(:,:,1:i),3);
end
 %  for i=1:h
% vv_(:,:,i)=sum(gg_(:,:,1:i),3);
% end
% for i=1:h
%     cont_istc(:,:,i)=ret2.S_IRF{1,i}(1:k,2)*ret2.S_IRF{1,i}(1:k,2)';%NOTICE THAT RIGHT NUM DETERMINES VARIABLE
%     weight_istc_WORLD(:,:,i)=sum(cont_istc(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));
% end
if cs==1
    ret2.total_res=ret2.total_res([k-1,1:k-2,k],:);
%     SIGMA=ret2.total_res*ret2.total_res'*(1/(T-p-(p-1)*m-1));
    SIGMA=ret2.SIGMA([k-1,1:k-2,k],[k-1,1:k-2,k]);
    pp=chol(SIGMA,'lower');ret2.p=pp;
    ret2.shocks=(pp\ret2.total_res)';
     parfor i=1:h
        R{i}=FF^i;
    end
            R{h}=FF^10000;
      for i=1:h-1
     ret2.S_IRF{1}=pp;
       ret2.S_IRF{i+1}=R{i}([k-1,1:k-2,k],[k-1,1:k-2,k])*pp;
       S_IRF1{1}=pp;
        S_IRF1{i+1}=[ret2.S_IRF{i+1}(1,:)-ret2.S_IRF{i}(1,:);ret2.S_IRF{i+1}(2,:);ret2.S_IRF{i+1}(3:k-st,:);ret2.S_IRF{i+1}(k-st+1:k,:)-ret2.S_IRF{i}(k-st+1:k,:)];
      end
                 ret2.S_IRF{h}=R{h}([k-1,1:k-2,k],[k-1,1:k-2,k])*pp;
        S_IRF1{h}=[ret2.S_IRF{h}(1,:)-ret2.S_IRF{h-1}(1,:);ret2.S_IRF{h}(2,:);ret2.S_IRF{h}(3:k-st,:);ret2.S_IRF{h}(k-st+1:k,:)-ret2.S_IRF{h-1}(k-st+1:k,:)];

   ret2.S_IRF=S_IRF1;
%   for i=2:h
%   ret2.S_IRF{i}=[ret2.S_IRF{i}(1,:)-ret2.S_IRF{i-1}(1,:);ret2.S_IRF{i}(2:k-2,:);ret2.S_IRF{i}(k-1,:)-ret2.S_IRF{i-1}(k-1,:);ret2.S_IRF{i}(k,:)]; 
%   end  
end
if fiscal==1
% ret3.total_res=ret2.total_res([1,3,2,4:k-1,k],:);%for tfp first
ret3.total_res=ret2.total_res([2,1,3:k-1,k],:);
    SIGMA=ret3.total_res*ret3.total_res'*(1/(T-(p-1)-(p-1)*m-1));
    pp=chol(SIGMA,'lower');
    ret2.shocks=(pp\ret3.total_res)';
    parfor i=1:h
        R{i}=FF^i;
    end
    
   for i=1:h-1
     ret3.S_IRF{1}=pp;
%        ret3.S_IRF{i+1}=R{i}([1,3,2,4:k-1,k],[1,3,2,4:k-1,k])*pp;%for tfp first
              ret3.S_IRF{i+1}=R{i}([2,1,3:k-1,k],[2,1,3:k-1,k])*pp;

        S_IRF1{1}=pp;
        S_IRF1{i+1}=ret3.S_IRF{i+1}(:,:)-ret3.S_IRF{i}(:,:);
   end
      ret3.S_IRF=S_IRF1;
end
% NOW COMPUTE TFP SHUT DOWN IRF FOR FISCAL PAPER;
if m==1
   a=eye(k,k);
else 
a=eye(k-1,k-1);
end
% for tfp paper;
% if m==1
%     ff5=[ret2.p'*a(:,end) a(:,1)];
% hh=null(ff5');
% % a(:,1) if g is first place; a(:,2) if g is second;
% ff6=hh*(hh'*a(:,2))/norm(hh'*a(:,2));
% for mfev paper;
if m==1
    ff5=[ret2.p'*a(:,end)];
hh=null(ff5');
% a(:,1) if g is first place; a(:,2) if g is second;
ff6=hh*(hh'*a(:,1))/norm(hh'*a(:,1));
else
% ff5=[ret2.p(1:k-1,1:k-1)'*[a(:,end) a(:,2)]];%IF YOU WANT Q_TFP TO BE ORTHOGONAL TO SECOND VARAIBLE in addition to last;
  ff5=[ret2.p(1:k-1,1:k-1)'*a(:,end)];%JEDC PAPER and any other paper other than gunn khan
%   ff5=[ret2.p(1:k-1,1:k-1)'*a(:,[1,2])];%GUNN KHAN PAPER

hh=null(ff5');
% a(:,1) if g is first place; a(:,2) if g is second;a(:,4) if fff is fourth;
% ff6=hh*(hh'*a(:,3))/norm(hh'*a(:,3));%gunn khan paper
 ff6=hh*(hh'*a(:,1))/norm(hh'*a(:,1));%JEDC PAPER

end
if m==1
    ff6=ff6;
else
ff6=[ff6;0];
end
 s_news_tfp=[ff6'/(ret2.S_IRF{1})*ret2.total_res(1:k,1:end)]';
for u=1:h
 IRF_NEWS_TFP(:,:,u)=ret2.S_IRF{u}*ff6;
 IRF_NEWS_TFP_(:,:,u)=IRF_NEWS_TFP(:,:,u)';
 cont_news_TFP(:,:,u)=IRF_NEWS_TFP(:,:,u)*IRF_NEWS_TFP(:,:,u)';
weight_news_TFP(:,:,u)=sum(cont_news_TFP(:,:,1:u),3)/(diag(diag(vv_(:,:,u))));
vd_tfp_news(u,:)=diag(weight_news_TFP(:,:,u));
end
IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
if m==1
    if mean(IRF_NEWS_TFP_(1:H,2))<0;
        IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
    s_news_tfp=-s_news_tfp;
%     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
    end
else
if mean(IRF_NEWS_TFP_(1:H,1))<0;
        IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
    s_news_tfp=-s_news_tfp;
%     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
end



% for i=1:h
%     gg_(:,:,i)=ret2.S_IRF{i}*ret2.S_IRF{i}';
% vv_(:,:,i)=sum(gg_(:,:,1:i),3);
% end
if jmcb_joint==0&&jedc_joint==0;
 j=1;%if j=1 it's sums of contributions; if it's j=H it's the contribution at H
 for i=j:H
% gg(:,:,i)=(sum(1./vv_(b,b,i:H),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%NADAV LIKE FRANCIS DICECIO ET.AL ABSOLUTE SHARE
% gg(:,:,i)=(sum(1./(vv_(b,b,i:H)-cont_istc(b,b,i:H)),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}( b,:);%NADAV_RELATIVE_SHARE
% gg(:,:,i)=1./vv_(b,b,i)*(H+1-i)*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%sims
gg(:,:,i)=(H+1-i)*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%uhlig
 end
 
  V=sum(gg(:,:,j:end),3);
  V=V(x:k,x:k);
[D E]=eig(V);rr=diag(E);
[r index] = sort(diag(E), 'ascend');vv=rr([index]);
 E2=diag(vv);
 D1=D(:,index);d=D1(:,end);d_=[zeros(x-1,1);d];
end
if jmcb_joint==1
   clear  IRF_NEWS_TFP_;
a=eye(k-1,k-1);
%     repl=10;
if x1==x2
   DD=randn(k-x1,b,repl);
else
    DD=randn(k-x2,b,repl);
end   
%    DD2=randn(k-x2,1,repl);
   j=1;
%       THIS IS THE MFEV FOR THE SECOND ORDERED VARIABLE

   for u=j:H2
%gg(:,:,i)=(sum(1./vv_(b,b,i:H2),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%NADAV LIKE FRANCIS DICECIO ET.AL ABSOLUTE SHARE
% gg(:,:,i)=(sum(1./(vv_(b,b,i:H)-cont_istc(b,b,i:H)),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}( b,:);%NADAV_RELATIVE_SHARE
 %gg(:,:,i)=1./vv_(b,b,i)*(H2+1-i)*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%sims
 gg(:,:,u)=(H2+1-u)*ret2.S_IRF{1,u}(x4,:)'*ret2.S_IRF{1,u}(x4,:);%uhlig
% gg_tfp(:,:,u)=(H+1-u)*ret2.S_IRF{1,u}(b-1,:)'*ret2.S_IRF{1,u}(b-1,:);%uhlig
   end 
%    THIS IS THE MFEV FOR THE FIRST ORDERED VARIABLE
    for u=j:H1
 %gg_tfp(:,:,i)=(sum(1./vv_(b,b,i:H1),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%NADAV LIKE FRANCIS DICECIO ET.AL ABSOLUTE SHARE
% gg(:,:,i)=(sum(1./(vv_(b,b,i:H)-cont_istc(b,b,i:H)),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}( b,:);%NADAV_RELATIVE_SHARE
% gg_tfp(:,:,i)=1./vv_(b-1,b-1,i)*(H1+1-i)*ret2.S_IRF{1,i}(b-1,:)'*ret2.S_IRF{1,i}(b-1,:);%sims
% gg(:,:,u)=(H+1-u)*ret2.S_IRF{1,u}(b,:)'*ret2.S_IRF{1,u}(b,:);%uhlig
gg_tfp(:,:,u)=(H1+1-u)*ret2.S_IRF{1,u}(x3,:)'*ret2.S_IRF{1,u}(x3,:);%uhlig
    end 
  if x1==x2
        V=sum(gg(:,:,j:end),3);
  V=V(x2:k-1,x2:k-1);
%   V(x2,:)=0;  V(:,x2)=0;
  V_TFP=sum(gg_tfp(:,:,j:end),3);
  V_TFP=V_TFP(x1:k-1,x1:k-1);
  else
        V=sum(gg(:,:,j:end),3);
  V=V(1:k-1,1:k-1);
%   V(x2,:)=0;  V(:,x2)=0;
  V_TFP=sum(gg_tfp(:,:,j:end),3);
  V_TFP=V_TFP(1:k-1,1:k-1);
  end
%     V_TFP(x1,:)=0;  V_TFP(:,x1)=0;

% for i=1:repl
% [D21(:,:,i) R1(:,:,i)]=qr(DD(:,:,i),0);
% r1(:,:,i)=diag(R1(:,:,i));
% D22(:,:,i)=(D21(:,:,i)*diag(r1(:,:,i)./r1(:,:,i)))'; 
%   obj_fun(i,:)=D22(1,:,i)*V*D22(1,:,i)' +D22(2,:,i)*V_TFP*D22(2,:,i)';
% end
[obj_fun,D24]=obj_fun1(DD,V,V_TFP,repl,x1,x2,ret2,H2,jie);
  [t tt]=sort(obj_fun);
  if x1~=x2;
        d=D24(1,:,tt(end,:))';
  d_=[d;0];
    dtfp=D24(2,:,tt(end,:))';
    d_tfp=[dtfp;0];
  else
        d=D24(1,:,tt(end,:))';
       d_=[zeros(x2-1,1);d;0];
%     dtfp=D24(2,:,tt(end,:))';%if two shocks use this
if b==1
        dtfp=D24(1,:,tt(end,:))';%if one shock use this
else
            dtfp=D24(2,:,tt(end,:))';%if one shock use this
end
    d_tfp=[zeros(x1-1,1);dtfp;0];
  end
%    display(size(d_tfp,1))
 s_news_tfp=[d_tfp'/(ret2.S_IRF{1})*ret2.total_res(1:k,1:end)]';
for u=1:h
 IRF_NEWS_TFP(:,:,u)=ret2.S_IRF{u}*d_tfp;
 IRF_NEWS_TFP_(:,:,u)=IRF_NEWS_TFP(:,:,u)';
 cont_news_TFP(:,:,u)=IRF_NEWS_TFP(:,:,u)*IRF_NEWS_TFP(:,:,u)';
weight_news_TFP(:,:,u)=sum(cont_news_TFP(:,:,1:u),3)/(diag(diag(vv_(:,:,u))));
vd_tfp_news(u,:)=diag(weight_news_TFP(:,:,u));
end
if jmcb_joint==0
IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
if mean(IRF_NEWS_TFP_(1:H,b-1))<0;
        IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
    s_news_tfp=-s_news_tfp;
%     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
else
    IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
if mean(IRF_NEWS_TFP_(1:H1,x3))<0;
        IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
    s_news_tfp=-s_news_tfp;
%     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
end

end
%  a_=ret2.S_IRF{1,1}(1:k,1:k)*d_;
if jedc_joint==1;%IF X1=X2 IT'S EJ PAPER; IF NOT IT'S JEDC PAPER;
%    clear  IRF_NEWS_TFP_;
a=eye(k-1,k-1);
%     repl=10;
if x1==x2
   DD=randn(k-3,1,repl);
else
       DD=randn(k-1,1,repl);
end
    %    DD2=randn(k-x2,1,repl);
   j=1;
%       THIS IS THE MFEV FOR THE SECOND ORDERED VARIABLE

   for u=j:H2
% gg(:,:,i)=(sum(1./vv_(b,b,i:H),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%NADAV LIKE FRANCIS DICECIO ET.AL ABSOLUTE SHARE
% gg(:,:,i)=(sum(1./(vv_(b,b,i:H)-cont_istc(b,b,i:H)),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}( b,:);%NADAV_RELATIVE_SHARE
% gg(:,:,i)=1./vv_(b,b,i)*(H+1-i)*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%sims
gg(:,:,u)=(H2+1-u)*ret2.S_IRF{1,u}(b,:)'*ret2.S_IRF{1,u}(b,:);%uhlig
% gg_tfp(:,:,u)=(H+1-u)*ret2.S_IRF{1,u}(b-1,:)'*ret2.S_IRF{1,u}(b-1,:);%uhlig
   end 
%    THIS IS THE MFEV FOR THE FIRST ORDERED VARIABLE
    for u=j:H1
% gg(:,:,i)=(sum(1./vv_(b,b,i:H),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%NADAV LIKE FRANCIS DICECIO ET.AL ABSOLUTE SHARE
% gg(:,:,i)=(sum(1./(vv_(b,b,i:H)-cont_istc(b,b,i:H)),3))*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}( b,:);%NADAV_RELATIVE_SHARE
% gg(:,:,i)=1./vv_(b,b,i)*(H+1-i)*ret2.S_IRF{1,i}(b,:)'*ret2.S_IRF{1,i}(b,:);%sims
% gg(:,:,u)=(H+1-u)*ret2.S_IRF{1,u}(b,:)'*ret2.S_IRF{1,u}(b,:);%uhlig
gg_tfp(:,:,u)=(H1+1-u)*ret2.S_IRF{1,u}(b-1,:)'*ret2.S_IRF{1,u}(b-1,:);%uhlig
    end 
  V=sum(gg(:,:,j:end),3);
  if x1==x2
  V=V(3:k-1,3:k-1);
%   V(x2,:)=0;  V(:,x2)=0;
  V_TFP=sum(gg_tfp(:,:,j:end),3);
  V_TFP=V_TFP(3:k-1,3:k-1);
  else
       V=V(1:k-1,1:k-1);
%   V(x2,:)=0;  V(:,x2)=0;
  V_TFP=sum(gg_tfp(:,:,j:end),3);
  V_TFP=V_TFP(1:k-1,1:k-1);
  end
%     V_TFP(x1,:)=0;  V_TFP(:,x1)=0;

% for i=1:repl
% [D21(:,:,i) R1(:,:,i)]=qr(DD(:,:,i),0);
% r1(:,:,i)=diag(R1(:,:,i));
% D22(:,:,i)=(D21(:,:,i)*diag(r1(:,:,i)./r1(:,:,i)))'; 
%   obj_fun(i,:)=D22(1,:,i)*V*D22(1,:,i)' +D22(2,:,i)*V_TFP*D22(2,:,i)';
% end
[obj_fun,D24]=obj_fun_jedc(DD,V,V_TFP,repl,x1,x2,a,k);
  [t tt]=sort(obj_fun);
  if x1~=x2;
        d=D24(:,1,tt(end,:));
  d_=[d;0];
%     dtfp=D24(2,:,tt(end,:))';
%     d_tfp=[dtfp;0];
  else
        d=D24(:,1,tt(end,:));
       d_=[0;0;d;0];
%     dtfp=D24(2,:,tt(end,:))';
%     d_tfp=[zeros(x1-1,1);dtfp;0];
  end
%   display(size(d_tfp,1))
%  s_news_tfp=[d_tfp'/(ret2.S_IRF{1})*ret2.total_res(1:k,1:end)]';
% for u=1:h
%  IRF_NEWS_TFP(:,:,u)=ret2.S_IRF{u}*d_tfp;
%  IRF_NEWS_TFP_(:,:,u)=IRF_NEWS_TFP(:,:,u)';
%  cont_news_TFP(:,:,u)=IRF_NEWS_TFP(:,:,u)*IRF_NEWS_TFP(:,:,u)';
% weight_news_TFP(:,:,u)=sum(cont_news_TFP(:,:,1:u),3)/(diag(diag(vv_(:,:,u))));
% vd_tfp_news(u,:)=diag(weight_news_TFP(:,:,u));
% end
% if jmcb_joint==0
% IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
% if mean(IRF_NEWS_TFP_(1:H,b-1))<0;
%         IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
%     s_news_tfp=-s_news_tfp;
% %     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
% end
% else
%     IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
% if mean(IRF_NEWS_TFP_(1:H1,b-1))<0;
%         IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
%     s_news_tfp=-s_news_tfp;
% %     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
% end
% end

end

% display(d_)
 s_news_WORLD=d_'/(ret2.S_IRF{1})*ret2.total_res(1:k,1:end);
      
%  ADD IS THE EMPIRICAL COUNTERPART OF AD FROM THEORETICAL MODEL - 7TH ROW IN INVERSE OF AD SHOULD BE EQUAL TO ADD;
% MATRIX THAT MULTIPIES REDUCED FORM SHOCKS
%  ADD=d_'/(ret2.S_IRF{1,1}(1:k,1:k));
%  MATRIX THAT MULTIPIES STRUCTURAL SHOCKS (THIS NEED TO BE COMPARED TO AD
%  ITSELF AND NOT INVERSE;
 ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) d_(1:k-1)];
 s_news_WORLD=s_news_WORLD';
%  PREALLOCATION OF MEMORY
 IRF_NEWS_WORLD=zeros(k,1,h);IRF_NEWS_WORLD_=zeros(1,k,h);
  IRF_TFP_WORLD=zeros(k,1,h);IRF_TFP_WORLD_=zeros(1,k,h);
   IRF_istc_WORLD=zeros(k,1,h);IRF_istc_WORLD_=zeros(1,k,h);
 cont_news_WORLD=zeros(k,k,h);weight_news_WORLD=zeros(k,k,h);
  cont_tfp=zeros(k,k,h);weight_tfp_WORLD=zeros(k,k,h);
   cont_istc=zeros(k,k,h);weight_istc_WORLD=zeros(k,k,h);
      vd_istc=zeros(h,k);vd_tfp=zeros(h,k);vd=zeros(h,k);
for i=1:h
 IRF_NEWS_WORLD(:,:,i)=ret2.S_IRF{i}*d_;
 IRF_NEWS_WORLD_(:,:,i)=IRF_NEWS_WORLD(:,:,i)';
end
if jmcb_joint==0
IRF_NEWS_WORLD_=squeeze(IRF_NEWS_WORLD_)';
if mean(IRF_NEWS_WORLD_(1:H,b))<0;
        IRF_NEWS_WORLD_=-IRF_NEWS_WORLD_;
    s_news_WORLD=-s_news_WORLD;
    ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
else
    IRF_NEWS_WORLD_=squeeze(IRF_NEWS_WORLD_)';
if mean(IRF_NEWS_WORLD_(1:H2,x4))<0;
        IRF_NEWS_WORLD_=-IRF_NEWS_WORLD_;
    s_news_WORLD=-s_news_WORLD;
    ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
end

if jie==1&&jmcb_joint==1
% IRF_NEWS_TFP_=squeeze(IRF_NEWS_TFP_)';
if mean(IRF_NEWS_TFP_(1:1,1))<0;
        IRF_NEWS_TFP_=-IRF_NEWS_TFP_;
    s_news_tfp=-s_news_tfp;
%     ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
%  IRF_NEWS_WORLD_=squeeze(IRF_NEWS_WORLD_)';
if mean(IRF_NEWS_WORLD_(1:1,1))<0;
        IRF_NEWS_WORLD_=-IRF_NEWS_WORLD_;
    s_news_WORLD=-s_news_WORLD;
    ADD=[ret2.S_IRF{1,1}(1:k-1,1:k-1) -d_(1:k-1)];
end
end




if cs==1
     cont_tfp(:,:,h)=ret2.S_IRF{h}(1:k,1)*ret2.S_IRF{h}(1:k,1)';
    weight_tfp_WORLD(:,:,h)=sum(cont_tfp(:,:,h),3)/(diag(diag(gg_(:,:,h))));
end
%IF YOU Q_FA FOR PAPPA TFP PAPER PUT INDEX=1 IN istc and index 3 in tfp;
if fiscal==0
   for i=1:h 
  IRF_TFP_WORLD(:,:,i)=ret2.S_IRF{1,i}(:,1);
IRF_TFP_WORLD_(:,:,i)=IRF_TFP_WORLD(:,:,i)';  
IRF_istc_WORLD(:,:,i)=ret2.S_IRF{1,i}(:,2);%NOTICE THAT COLUMN NUMBER DETERMINES VARIABLE
IRF_istc_WORLD_(:,:,i)=IRF_istc_WORLD(:,:,i)';
   end
else    
   for i=1:h 
  IRF_TFP_WORLD(:,:,i)=ret2.S_IRF{1,i}(:,3);
IRF_TFP_WORLD_(:,:,i)=IRF_TFP_WORLD(:,:,i)';
IRF_istc_WORLD(:,:,i)=ret3.S_IRF{1,i}(:,2);%NOTICE THAT COLUMN NUMBER DETERMINES VARIABLE (put 3 for THE CASE WHEN TFP IS FIRST IN PAPPA PAPER)
IRF_istc_WORLD_(:,:,i)=IRF_istc_WORLD(:,:,i)';
    end
end
IRF_TFP_WORLD_=squeeze(IRF_TFP_WORLD_)';
IRF_istc_WORLD_=squeeze(IRF_istc_WORLD_)';

  for i=1:h
cont_news_WORLD(:,:,i)=IRF_NEWS_WORLD(:,:,i)*IRF_NEWS_WORLD(:,:,i)';
weight_news_WORLD(:,:,i)=sum(cont_news_WORLD(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));

    cont_tfp(:,:,i)=IRF_TFP_WORLD(:,:,i)*IRF_TFP_WORLD(:,:,i)';
    weight_tfp_WORLD(:,:,i)=sum(cont_tfp(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));
if fiscal==0
    cont_istc(:,:,i)=IRF_istc_WORLD(:,:,i)*IRF_istc_WORLD(:,:,i)';%NOTICE THAT RIGHT NUM DETERMINES VARIABLE
    weight_istc_WORLD(:,:,i)=sum(cont_istc(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));
else
     cont_istc(:,:,i)=ret3.S_IRF{1,i}([2,1,3:k],2)*ret3.S_IRF{1,i}([2,1,3:k],2)';%NOTICE THAT RIGHT NUM DETERMINES VARIABLE
%     cont_istc(:,:,i)=IRF_istc_WORLD(:,:,i)*IRF_istc_WORLD(:,:,i)';
    weight_istc_WORLD(:,:,i)=sum(cont_istc(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));
    cont_tfp(:,:,i)=IRF_TFP_WORLD(:,:,i)*IRF_TFP_WORLD(:,:,i)';
    weight_tfp_WORLD(:,:,i)=sum(cont_tfp(:,:,1:i),3)/(diag(diag(vv_(:,:,i))));
   end

end  
parfor i=1:h
vd(i,:)=diag(weight_news_WORLD(:,:,i));
vd_istc(i,:)=diag(weight_istc_WORLD(:,:,i));
vd_tfp(i,:)=diag(weight_tfp_WORLD(:,:,i));
end
% if mean(IRF_TFP_WORLD_(2:12,1))>=0;
%     IRF_TFP_WORLD_=IRF_TFP_WORLD_;
%     s_tfp=s_tfp;
% else
%     IRF_TFP_WORLD_=-IRF_TFP_WORLD_;
%     s_tfp=-s_tfp;
% end
% if mean(IRF_istc_WORLD_(2:12,2))>=0;
%     IRF_istc_WORLD_=IRF_istc_WORLD_;
%     s_istc=s_istc;
% else
%     IRF_istc_WORLD_=-IRF_istc_WORLD_;
%     s_istc=-s_istc;
% end
%CONDITIONAL CORRELATION CALCULATION
vd_=vd;
clear g*  E*   V  G*
usa=ret2;usa.vd=vd_;
usa.news=IRF_NEWS_WORLD_;
usa.TFP=IRF_TFP_WORLD_;
usa.rr=s_news_WORLD;
% if jmcb_joint==1||fiscal_new;
usa.vd_tfp_news=vd_tfp_news;
usa.news_tfp=IRF_NEWS_TFP_;
usa.news_tfp_series=s_news_tfp;
% end
% usa.weight_news=weight_news_WORLD;
usa.istc=IRF_istc_WORLD_;
usa.weight_tfp=vd_tfp;usa.weight_istc=vd_istc;usa.d=d_;
usa.max_eig=ret.max_eig;usa.trace_stat=ret.trace_stat;usa.COINT_VECTOR=ret.b_;
 clear res*   c1 c2 coint* sig* r1 r2 v E sh* D*
